<template>
  <el-dialog v-model="visible" title="编辑活动" :before-close="cancel">
    <el-form :model="form" label-width="80px">
      <el-form-item label="活动标题:">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="开始时间:">
        <el-date-picker
          v-model="form.startDate"
          type="date"
          placeholder="请选择时间"
        />
      </el-form-item>
      <el-form-item label="结束时间:">
        <el-date-picker
          v-model="form.endDate"
          type="date"
          placeholder="请选择时间"
        />
      </el-form-item>
      <el-form-item label="上线/下线">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1" size="large">上线</el-radio>
          <el-radio :label="0" size="large">下线</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="isEdit(form.id!)">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import type { Ref } from "vue";
import { ref, withDefaults, watch } from "vue";
import * as http from "@/request/api";
import EventBus from "@/utils";

const props = withDefaults(
  defineProps<{ visible: boolean; data: FlashListDataRes }>(),
  {
    visible: false,
  }
);
const form = ref({}) as Ref<FlashListDataRes>;
watch(
  () => props.data,
  (val) => {
    console.log(val);
    form.value = { ...val };
  },
  { deep: true }
);

// 声明emit
const emit = defineEmits<{
  (event: "close"): void;
}>();
// 关闭方法
function cancel() {
  emit("close");
}

// 点击确认编辑
async function isEdit(id: number) {
  console.log(id);
  await http.flash.editFlashUpdate(id, form.value);
  cancel();
  EventBus.emit("isEdit", {});
}
</script>
